
import ParserError from "@/js/GraphParser/src/ParserError"
import { GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import { InputLinkNodeAny, InputLinkNodeBoolean, InputLinkNodeFunction, InputLinkNodeGroup, InputLinkNodeNumber, InputLinkNodeObject, InputLinkNodeString } from "@/js/components/InputLinkNode"

export class New extends GraphNode{
    group?: InputLinkNodeGroup
    load(){
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView, false, false)
        this.layout = layout
        this.appendChild(layout.container)
        
        // 引用
        layout.main?.left.appendChild(new InputLinkNodeAny(this, this.graphView as GraphView, {
            name: "class", 
            desc: '类',
            model: 'entry',
            showInput: false,
        }))

        // 引用
        layout.main?.right.appendChild(new InputLinkNodeFunction(this, this.graphView as GraphView, {
            name: "quote", 
            desc: '对象',
            model: "output",
            showInput: false,
        }))

        const group = new InputLinkNodeGroup( this, this.graphView as GraphView , {
            name: 'arguments'
        })
        .addInputLinkNodes([
            {
                name: '',
                desc: '字符串(string)',
                model: 'entry',
                inputTypeString: true,
                placeholder: '值',
                inputValue: '',
                isBindValue: false,
                InputLinkNodeCLass: InputLinkNodeString,
                title: '字符串(string)'
            },
            {
                name: '',
                desc: '数值(number)',
                model: "entry",
                placeholder: '值',
                isBindValue: false,
                InputLinkNodeCLass: InputLinkNodeNumber,
                title: '数值(number)'
            },
            {
                name: '',
                desc: '布尔(boolean)',
                model: "entry",
                placeholder: '值',
                isBindValue: false,
                InputLinkNodeCLass: InputLinkNodeBoolean,
                title: '布尔(boolean)'
            },
            {
                name: '',
                desc: '对象(object)',
                model: "entry",
                placeholder: '值',
                isBindValue: false,
                showInput: false,
                InputLinkNodeCLass: InputLinkNodeObject,
                title: '对象(object)',
                
            },
            {
                name: '',
                desc: '函数(function)',
                model: "entry",
                showInput: false,
                placeholder: '值',
                isBindValue: false,
                InputLinkNodeCLass: InputLinkNodeFunction,
                title: '函数(function)',
            },
            {
                name: '',
                desc: '通用(any)',
                model: "entry",
                inputTypeString: true,
                placeholder: '值',
                isBindValue: false,
                InputLinkNodeCLass: InputLinkNodeAny,
                title: '通用(any)',
            }
        ])
        layout.main?.left.appendChild(group)
        
        this.group = group
    }

    import(obj:any){
        this.group?.import(obj.userData.arguments)
        super.import(obj)
    }
}

New.parser = ({next, node, typeConversion })=>{
    const Class = next(node.enters.class)

    if(!Class){
        throw new ParserError(node.uuid, '未选择类对象，将造成异常错误！')
    }

    // 解析参数
    const argumentList:string[] = []

    ;(node.userData.arguments||[]).forEach((item: { data: any, uuid: string }) => {
        // 通过uuid查找连接节点
        argumentList.push(next(node.enters[item.uuid], typeConversion(item.data) ))
    })
    
    return `new ${Class}(${argumentList.join(",")})`
}